﻿using GKQC.Common;
using GKQC.DbHelper.Dao;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.ViewModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GKQC.DbHelper.Services
{
    public class BaseDataTypeService
    {
        AllDbContext dbContext;

        public static List<ItemTypeView> GetBillTypes(AllDbContext dbContext)
        {
            var types = new List<ItemTypeView>();
            var items = new List<BaseDataModel>();
            var ipqc = GetUseDatas(dbContext, "PQC");
            if (ipqc != null && ipqc.Count > 0)
            {
                ipqc.ForEach(t =>
                {
                    var m = new ItemTypeView();
                    m.TypeCode  = t.TypeCode;
                    m.TypeName = t.Name;
                    m.Items = t.Code;
                    types.Add(m);
                });
            }
            var iqc = GetUseDatas(dbContext, "IQC");
            if (iqc != null && iqc.Count > 0)
            {
                iqc.ForEach(t =>
                {
                    var m = new ItemTypeView();
                    m.TypeCode = t.TypeCode;
                    m.TypeName = t.Name;
                    m.Items = t.Code;
                    types.Add(m);
                });
            }

            var Oqc = BaseDataTypeService.GetUseDatas(dbContext, "OQC");
            if (Oqc != null && Oqc.Count > 0)
            {
                Oqc.ForEach(t =>
                {
                    var m = new ItemTypeView();
                    m.TypeCode = t.TypeCode;
                    m.TypeName = t.Name;
                    m.Items = t.Code;
                    types.Add(m);
                });
            }

            var ORT = BaseDataTypeService.GetUseDatas(dbContext, "ORT");
            if (ORT != null && ORT.Count > 0)
            {
                ORT.ForEach(t =>
                {
                    var m = new ItemTypeView();
                    m.TypeCode = t.TypeCode;
                    m.TypeName = t.Name;
                    m.Items = t.Code;
                    types.Add(m);
                });
            }

            //信赖性对比试验
            var COM = BaseDataTypeService.GetUseDatas(dbContext, "COM");
            if (COM != null && COM.Count > 0)
            {
                COM.ForEach(t =>
                {
                    var m = new ItemTypeView();
                    m.TypeCode = t.TypeCode;
                    m.TypeName = t.Name;
                    m.Items = t.Code;
                    types.Add(m);
                });
            }

            return types;
        }

        public BaseDataTypeService(AllDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public List<BaseDataModel> GetAllDatas()
        {
            return GetAllDatas(dbContext);
        }

        public static List<BaseDataModel> GetAllDatas(AllDbContext dbContext)
        {
            return dbContext.QcDb.Queryable<BaseDataModel>()
                .ToList();
        }

        public List<BaseDataModel> GetDatas(string typecode)
        {
            return GetDatas(dbContext,typecode);
        }

        public static List<BaseDataModel> GetDatas(AllDbContext dbContext, string typecode)
        {
            return dbContext.QcDb.Queryable<BaseDataModel>()
                .Where(d => d.TypeCode == typecode)
                .OrderBy(t => t.Group)
                .OrderBy(t => t.SNo)
                .ToList();
        }

        public List<BaseDataModel> GetUseDatas(string typecode)
        {
            return GetUseDatas(dbContext, typecode);
        }

        public static List<BaseDataModel> GetUseDatas(AllDbContext dbContext, string typecode)
        {
            return dbContext.QcDb.Queryable<BaseDataModel>()
                .Where(d => d.TypeCode == typecode && d.IsUse == 1)
                .OrderBy(d=>d.Group)
                .OrderBy(d => d.SNo)
                .ToList();
        }

        public List<BaseDataTypeModel> GetAllTypes()
        {
            return GetAllTypes(dbContext);
        }
        public static List<BaseDataTypeModel> GetAllTypes(AllDbContext dbContext)
        {
            return dbContext.QcDb.Queryable<BaseDataTypeModel>()
                .OrderBy(T => T.TypeCode)
                .ToList();
        }

        public List<BaseDataTypeModel> GetAllUseTypes()
        {
            return GetAllUseTypes(dbContext);
        }
        public static List<BaseDataTypeModel> GetAllUseTypes(AllDbContext dbContext)
        {
            return dbContext.QcDb.Queryable<BaseDataTypeModel>()
                .Where(t => t.IsUse == 1)
                .OrderBy(T => T.TypeCode)
                .ToList();
        }

        public bool SaveData(List<BaseDataModel> items)
        {
            if (items == null || items.Count == 0) return true;

            bool flag = true;

            dbContext.QcDb.Saveable(items).ExecuteCommand();

            return flag;
        }
    }
}
